<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>038-设计模式-单例模式-皇帝上朝.html</title>
	<script>
		/*
		(function(w){
			function King(name){
				this.name = name;
				this.id = Math.random();
			}
			King.prototype = {
				constructor:King,
				work:function(){
					console.log(this.name + this.id + '说:单例模式')
				}
			}
			w.King = King;
		})(window);
		
		for(var i = 1;i<=5;i++){
			new King('我').work();
		}
		*/
		(function(w){
			var king = null;
			function King(name){
				if(!king){
					this.name = name;
					this.id = Math.random();
					king = this;
				}
				return king;
			}
			King.prototype = {
				constructor:King,
				work:function(){
					console.log(this.name + this.id + '说:单例模式')
				}
			}
			w.King = King;
		})(window);
		
		for(var i = 1;i<=5;i++){
			// console.log(new King('我'));
			new King('我').work();
		}

	</script>
</head>
<body>
	
</body>
</html>